<template>
  <div class="dashboard-container">
    <header>
      <el-form :inline="true" :model="searchFrom" class="demo-form-inline">
        <el-form-item label="编号">
          <el-input
            v-model="searchFrom.code"
            placeholder="请输入编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="名称">
          <el-input
            v-model="searchFrom.name"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="地理位置">
          <div style="display: flex">
            <el-input
              v-model="searchFrom.city"
              placeholder="请输入所在市"
            ></el-input>
            <el-input
              v-model="searchFrom.area"
              placeholder="请输入所在区"
            ></el-input>
            <el-input
              v-model="searchFrom.address"
              placeholder="请输入详细位置"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </header>
    <main>
      <div style="display: flex; justify-content: flex-end; margin-right: 65px">
        <el-button type="success" @click="toAdd()">录入</el-button>
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="code" label="编号" width="280">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="280">
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="280"> </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="toEdit(scope.row)" type="primary" size="small"
              >编辑</el-button
            >
            <el-button @click="toDelete(scope.row)" type="danger" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="100"
        @current-change="currentChange"
      >
      </el-pagination>
    </main>
    <module
      :title="title"
      :visible="visible"
      @cancel="cancel"
      @getTreeList="getTreeList"
      :treeInfo="treeInfo"
    ></module>
  </div>
</template>

<script>
import module from "./module/module.vue";
import { getTreeList, deleteTree } from "@/api/tree";
import { mapGetters } from "vuex";

export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["name"]),
  },
  components: {
    module,
  },
  data() {
    return {
      visible: false,
      title: "新增",
      treeInfo: {},
      editID: "",
      deleteID: "",
      searchFrom: {
        code: "",
        name: "",
        city: "",
        area: "",
        address: "",
        currentPage: 1,
        pageSize: 5,
      },
      tableData: [],
    };
  },
  mounted() {
    this.getTreeList();
  },
  methods: {
    getTreeList() {
      getTreeList(this.searchFrom).then((res) => {
        console.log(res);
        this.tableData = res.data;
      });
    },
    toAdd() {
      this.title = "新增";
      this.visible = true;
    },
    toEdit(row) {
      this.treeInfo = row;
      console.log(this.treeInfo);
      this.editID = row.id;
      console.log(row);
      this.title = "编辑";
      this.visible = true;
    },
    toDelete(row) {
      this.deleteID = row.id;
      let that = this;
      this.$confirm("是否要将该数据删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteTree({ id: that.deleteID }).then((res) => {
            console.log(res);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            that.getTreeList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    cancel() {
      this.visible = false;
    },
    onSubmit() {
      console.log(this.searchFrom);
      this.getTreeList();
    },
    reset() {
      this.searchFrom = {
        code: "",
        name: "",
        city: "",
        area: "",
        address: "",
        currentPage: 1,
        pageSize: 5,
      };
      this.getTreeList();
    },
    currentChange(e) {
      console.log(e);
      this.searchFrom.currentPage = e;
      this.getTreeList();
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
